home *** CD-ROM | disk | FTP | other *** search
- Path: news.mira.net.au!news
- From: davidw@werple.net.au (David White)
- Newsgroups: comp.lang.c++
- Subject: Re: polymorphism and style
- Date: 3 Mar 1996 12:18:30 +1100
- Organization: Werple Internet, Melbourne
- Message-ID: <4has16$cge@werple.net.au>
- References: <4gr4ur$n13@alpha.pcix.com> <DnI6II.KMt@news.arco.com> <4h4h3h$6ur@brtph500.bnr.ca>
- NNTP-Posting-Host: werple.mira.net.au
-
- oophelp@bnr.ca (OOP Helpline) writes:
-
-
- >|> > Question on polymorphism and style.
- >|> >Want to call a function in a most derived class off a pointer to the base
- >|> >class. The fuction does not exist in the base class.
- >|> >I should:
- >|> >1)downcast ?
- >|> >2)add the function to the base class and add virtual stubs to every
- >|> > class that does not use it ?
- >|> >3)add it to the base class but define it like
- >|> > foo( ..whatever..) {error_msg("virtual function not defined");}
- >|> > This way only the class that uses it would have to define it.
- >|> >4) ?
- >|> >
- >|> >thanks for your time.
-
- >|>but maybe you need to take another
- >|> look at your design.
-
- >Exactly, this is clearly a design fault. Upcasting a derived class object
- >to a base class requires that you only do base class operations on it.
- >(While any pilot can be a passenger on an airplane, any passenger cannot
- >be a pilot.)
-
- >If you need to keep the ability to do derived class behavior on a derived
- >class object, you need to keep a derived class reference to it
- >(i.e. use two pointers to the derived class object, one for use
- >for base class behavior and one for use for derived class behavior).
-
- There are many occasions when this is not practical. Suppose I have an
- array of base class object pointers; at some stage I might want to iterate
- over them, retrieve the derived classes, and call functions specific to the
- derived type. Or I might pass an object pointer to something and receive
- it back later and have to retrieve the derived class. These are exactly
- the kinds of situations that 'dynamic_cast' was intended for. Your
- suggestion can unnecessarily complicate the design, rather than simplify
- it. To state, without qualification, that "this is clearly a design fault"
- demonstrates ignorance of real situations in OO design. And this from an
- OO helpline!
-
- David White
- davidw@werple.mira.net.au
-
-